﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using SPFarmModeler.Common;
using SPFarmModeler.Business;
using SPFarmModeler.Forms;

namespace SPFarmModeler.UserControls
{
    public class ServicesAndServersGrid : UserControl
    {
        private TableLayoutPanel Table;
        private Farm cFarm;

        public void UpdateFarm(object sender, Farm f)
        {
            cFarm = f;
            if (cFarm != null)
            {
                // Get the list of the available SPService
                List<SPService> services = SPService.GetServices();

                Table.ColumnCount = 1;
                Table.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 300));

                Table.RowCount = services.Count + 1;

                // Creation of the first column (which contains the SP services)
                for (int i = 0; i < services.Count; i++)
                {
                    Label l = new Label();
                    l.AutoSize = true;
                    l.Text = services[i].Name;
                    Table.Controls.Add(l, 0, i + 1);
                }
                this.Visible = true;
            }
            else
            {
                Table.ColumnCount = 0;
                Table.RowCount = 0;
                Table.Controls.Clear();
                this.Visible = false;
            }
        }

        public ServicesAndServersGrid()
        {
            this.Visible = false;
            this.AutoSize = true;

            // Create the table panel
            Table = new TableLayoutPanel();
            Table.AutoSize = true;
            Table.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;

            this.Controls.Add(Table);
        }

        public void AddServer(string serverName)
        {
            if (cFarm == null) return;

            Table.Visible = false;
            Table.SuspendLayout();

            Label l = new Label();
            l.AutoSize = true;
            l.Text = serverName;

            // Creation of column associated to the new server
            Table.ColumnCount++;
            Table.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 100));
            Table.Controls.Add(l, Table.ColumnCount - 1, 0);

            // Insert a checkbox for each service
            for (int i = 1; i < Table.RowCount; i++)
            {
                Table.Controls.Add(new SPServiceCell(null), Table.ColumnCount - 1, i);
            }

            Table.ResumeLayout();
            Table.Visible = true;
        }
    }
}
